<?php
namespace App\Models\Api;
use App\Models\Like;
use Illuminate\Database\Eloquent\Model;
use App\Models\Api\Base;
class Comment extends Base
{  
    protected $table = 'comment';
    public $timestamps = false;
    // 不能被填充的字段
    protected $guarded = [];

    // 当前登录用户
    public function isLike($small = false,$uid)
    { 
        if(!$uid){
            return false;
        }
        if (false === $small) {
            return $this->likes->contains('uid', $uid);
        } else {
            return $this->likes()->where('uid',$uid)->first() ? true : false;
        }
    }
     // 多态关联获取评论的赞
    public function likes()
    {
        return $this->morphMany(Like::class, 'likeable');
    }
    // 多对一关联文章表, 获取评论文章
    public function community()
    {
        return $this->belongsTo(Community::class);
    }
    // 多对一关联用户表，获取评论者
    public function user()
    {
        return $this->belongsTo(\App\Models\Api\User::class,'uid','id')->select('id','uen','tel','uc_id','nickname','sex','avatar','status','has_self_recode','forbidden','regid');
    }
    // 多对一关联用户表,获取回复对象
    public function rUser()
    {
        return $this->belongsTo(\App\Models\Api\User::class, 'to_uid')->select('id','uen','tel','uc_id','nickname','sex','avatar','status','has_self_recode','forbidden','regid');
    }
    // 一对多关联评论表，获取子评论
    public function childs()
    {
        return $this->hasMany(Comment::class,'p_id');
    }
}